package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.URI;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.swing.AbstractButton;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;

import model.LogModel;
import view.AboutView;
import view.ExportView;
import view.LogView;
import view.OptionsView;

public class LogController {

	private ResourceBundle rLabels;
	private Properties configFile;
	private LogModel model;
	private LogView view;
	private OptionsView oView;
	private ExportView eView;
	private AboutView aView;

	public LogController(LogModel model, LogView view) {
		this.model = model;
		this.view = view;

		/* Configuration */
		configFile = model.loadConfiguration();
		view.setLanguage(model.getLanguage());

		/* Internationalization */
		rLabels = model.getLabels();
		view.setLabels(rLabels);

		initView();
	}
	
	public void initView() {
		view.setTitle(configFile.getProperty("applicationName"));
		view.createMenuBar();
		view.addFilterListener(new FilterListener());
		view.addLoadLogsListener(new LoadLogsListener());
		view.addWinListener(new AddWindowListener());
		view.getEditPreferencesItem().addActionListener(new EditPreferencesAction());
		view.getAboutItem().addActionListener(new AboutAction());
		view.getShowSBItem().addActionListener(new ShowSBAction());
		view.getExportItem().addActionListener(new ExportAction());
		view.getQuitItem().addActionListener(new QuitAction());
	}
	
	public void setLanguage(String sLanguage) {
		model.setLanguage(sLanguage);
		JOptionPane.showMessageDialog(view.getContent(), rLabels.getString("changesApplied"), configFile.getProperty("applicationName"), JOptionPane.INFORMATION_MESSAGE);
		oView.setVisible(false);
	}
	
	public Properties getConfigFile() {
		return configFile;
	}

	class FilterListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			String text = view.getTextFiltre();
			if (text.length() == 0) { view.getSorter().setRowFilter(null); }
			else {
				view.getSorter().setRowFilter(RowFilter.regexFilter(text.toUpperCase()));
			}
		}
	}
	
	class LoadLogsListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			view.setLogs(model.readLogs());
			view.getExportItem().setEnabled(true);
		}
	}
	
	class AddWindowListener implements WindowListener {
		@Override
		public void windowActivated(WindowEvent arg0) {}
		@Override
		public void windowClosed(WindowEvent arg0) {}
		@Override
		public void windowClosing(WindowEvent arg0) {
			int dialogResult = JOptionPane.showConfirmDialog(
					view.getContent(), 
					rLabels.getString("confirmExitText"), 
					rLabels.getString("confirmExitTitle"), 
					JOptionPane.YES_NO_OPTION);

			if(dialogResult == JOptionPane.YES_OPTION){ System.exit(0); }
		}
		@Override
		public void windowDeactivated(WindowEvent arg0) {}
		@Override
		public void windowDeiconified(WindowEvent arg0) {}
		@Override
		public void windowIconified(WindowEvent arg0) {}
		@Override
		public void windowOpened(WindowEvent arg0) {}		
	}
	
	class EditPreferencesAction implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			oView = new OptionsView(model);
			oView.setModal(true);
			oView.setLocationRelativeTo(view);
			oView.getSelectLanguage().setSelectedIndex(0);
			
			int index = 2;
			if(view.getLanguage().equalsIgnoreCase("ca")) index = 0;
			else if(view.getLanguage().equalsIgnoreCase("es")) index = 1;
			else if(view.getLanguage().equalsIgnoreCase("en")) index = 2;
			oView.getSelectLanguage().setSelectedIndex(index);
			
			oView.getApply().addActionListener(new ApplyPreferencesAction());
			oView.setVisible(true);
		}
	}
	
	class AboutAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			aView = new AboutView(model);
			aView.setModal(true);
			aView.setLocationRelativeTo(view);
			
			aView.getWeb().addActionListener(new OpenWebAction(aView.getUri()));
			aView.getLicense().addActionListener(new OpenWebAction(aView.getULicense()));
			aView.setVisible(true);
		}
	}
	
	class ApplyPreferencesAction implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			int iLanguage = oView.getSelectLanguage().getSelectedIndex();
			
			switch(iLanguage) {
				case 0: setLanguage("ca"); break;
				case 1: setLanguage("es"); break;
				case 2: setLanguage("en"); break;
				default: setLanguage("en"); break;
			}
		}
	}

	class ShowSBAction implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			AbstractButton aButton = (AbstractButton) event.getSource();
			boolean bChecked = aButton.getModel().isSelected();
			if (bChecked) {
				view.getStatusBar().setVisible(true);
			} else {
				view.getStatusBar().setVisible(false);
			}
		}
	}
	
	class ExportAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			eView = new ExportView(model);
			eView.setModal(true);
			eView.setLocationRelativeTo(view);
			
			eView.getExportPDFButton().addActionListener(new ExportPDFAction());
			eView.setVisible(true);
		}
	}
	
	class ExportPDFAction implements ActionListener {

		public void actionPerformed(ActionEvent e) {
			model.exportPDF(view.getLogs(), eView.getExportParams());
			eView.setVisible(false);
		}
		
	}

	class QuitAction implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int dialogResult = JOptionPane.showConfirmDialog(
					view.getContent(), 
					rLabels.getString("confirmExitText"), 
					rLabels.getString("confirmExitTitle"), 
					JOptionPane.YES_NO_OPTION);
			if(dialogResult == JOptionPane.YES_OPTION) { System.exit(0); }
		}
	}
	    
	    
	class OpenWebAction implements ActionListener {
		private URI uri;
		public OpenWebAction(URI uri) {
			this.uri = uri;
		}
		@Override
		public void actionPerformed(ActionEvent arg0) {
			model.open(uri);
		}
	}
}
